#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：BroadviewCoss
@File    ：page_data.py
@IDE     ：PyCharm
@Author  ：kdly
@Date    ：2025/3/18 下午1:59
'''
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage


class PageData:
    def __init__(self, data, size, page_num):
        self.__data = data
        self.__size = size
        self.__page_num = page_num

    def get_search(self):
        paginator = Paginator(self.__data, self.__size)
        try:
            page = paginator.page(self.__page_num)
        except PageNotAnInteger as e:
            page = paginator.page(1)
        except EmptyPage as e:
            if int(self.__page_num) > paginator.num_pages:
                # 大于最大页码，则返回最后一页数据
                page = paginator.page(paginator.num_pages)
            else:
                # 小于最小页码，则返回第一页数据
                page = paginator.page(1)
        user_list = list(page.object_list)
        return user_list
